/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var instanciado = false;
var timer;
$(document).ready(main);

function main(){
    $("#realizarSimulacion").click(iniciarSimulacion);  
    timer = setInterval(informePronostico(),2000);
}

function informePronostico(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url:"informePronostico.htm",
        success: function(data){
            if (data.me == "No corre"){
                clearInterval(timer);
                console.log(data);
                var fechaPronostico = data.fecha;
                var turno = data.turno;
                var turnoString;
                var hora = data.hora;
                
                $("#FechaPronostico").text(fechaPronostico);
                
                $("#HoraPronostico").text(hora);
                
                if (turno == 1){
                    turnoString = "Ma\u00f1ana";
                }
                else {
                    if (turno == 2){
                        turnoString = "Tarde";
                    }
                    else {
                        turnoString = "Noche";
                    }
                }
                
                $("#TurnoPronostico").text(turnoString);
                
                $("#modalPronostico").modal('toggle');
            }
        }
    });    
}


function iniciarSimulacion(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "iniciarPronostico.htm"
    });
    if (instanciado === true){        
        var canvases = [];
        canvases.push(motorGP.vehicleCanvas);      
        visualizer.draw();
    }  
    else{
        addCanvasP();
        instanciado = true;        
    }   
    
    motorGP.imagesLoaded = true;
}